package com.foreverwin.jzymes.production.remedy.service.impl;

import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.remedy.service.IClampCarrierAdviceService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.mapper.SFCMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClampCarrierAdviceServiceImpl implements IClampCarrierAdviceService {

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    ICarrierScanLogService carrierScanLogService;
    @Autowired
    SFCMapper sfcMapper;

    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public void clampCarrier( String carrier, String resrce ){
        String site = CommonMethods.getSite();
        /**检查站点*/
        siteAdviceService.existSite( site );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ));
        /**检查设备*/
        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );
        /**检查设备默认工序是否装夹工序*/
        String operation = resourceAdviceService.findOperationByResource( resourceMap );
        Date createdDateTime = new Date();

        List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( site, carrier );
        if( carrierBindList != null && carrierBindList.size() > 0 ){
            /**检查本体物料在机种中不存在，自动解绑本体*/
            if( !checkCarrierItemValid( site, carrierBindList ) ){
                carrierBindAdviceService.carrierUnBind( carrierBindList, createdDateTime );
            }else{
                throw new BusinessException( "production.remedy.carrierBinded", new String[]{ carrier } );
            }
        }

        /**本体绑定*/
        carrierBindAdviceService.carrierBind( site, carrier, createdDateTime );

        /**保存扫码记录*/
        CarrierScanLog carrierScanLog = new CarrierScanLog();
        carrierScanLog.setHandle( BOHandleUtil.buildCarrierScanLogHandle() );
        carrierScanLog.setSite( site );
        carrierScanLog.setMachineType( itemGroupAdviceService.findItemGroupBySite( site ) );
        carrierScanLog.setCarrier( carrier );
        carrierScanLog.setResrce( resrce );
        carrierScanLog.setOperation( operation );
        carrierScanLog.setResult( "1" );
        carrierScanLog.setPass( "Y" );
        carrierScanLog.setCreatedDateTime( createdDateTime );
        carrierScanLogService.insertCarrierScanLog( carrierScanLog );

        /**本体过站*/
        Map<String,String> sfcMap = commonService.sfcPassByCarrier( site, carrier, resrce, createdDateTime );

        /**更新工令已下达数量*/
        for( String itemBo : sfcMap.keySet() ){
            String sfc = sfcMap.get( itemBo );
            SFC _sfc = sfcMapper.selectSFCById( BOHandleUtil.buildSFCHandle( site, sfc ) );
            _sfc.setReleasedQty( _sfc.getReleasedQty() == null ? 1L : _sfc.getReleasedQty() + 1 );
            _sfc.setUpdatedDateTime( createdDateTime );
            if( _sfc.getActStartTime() == null ){
                _sfc.setActStartTime( createdDateTime );
                _sfc.setStatusBo( "StatusBO:"+ site +",403" );
            }
            sfcMapper.updateSFC( _sfc );
        }
    }

    /**
     * 检查本体物料在机种是否存在
     *
     * @param site
     * @param carrierBindList
     * @return
     */
    public boolean checkCarrierItemValid( String site, List<CarrierBind> carrierBindList ){
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemGroupMemberList = itemGroupAdviceService.findItemGroupMember( site, itemGroup );
        for( CarrierBind carrierBind : carrierBindList ){
            boolean existed = false;
            for( Map<String,Object> itemGroupMember : itemGroupMemberList ){
                if( carrierBind.getItemBo().equals( itemGroupMember.get( "ITEM_BO" ) ) ){
                    existed = true;
                    break;
                }
            }
            if( !existed ){
                return false;
            }
        }
        return true;
    }
}
